import {ref} from 'vue';
import {defineStore} from 'pinia';
import type {UserInfo} from '../types';
import {storage} from '../utils/storage'
import {getUser} from "../api";


export const useUserStore = defineStore('user', () => {
    const userInfo = ref<any | null>(null);

    // const isLogin = computed(() => !!userInfo.value)
    const isLogin = true
    const setUserCache = (user: UserInfo) => {
        storage('username').set(user.username)
        storage('token').set(user.token)
    };

    const verifyAuth = async () => {
        if (storage('username').get() != null && storage('token').get() != null) {
            await getUser()
            // setUserCache(res.data)
        }
    };

    const removeUser = () => {
        storage('username').remove()
        storage('token').remove()
    }

    return {
        userInfo,
        setUserCache,
        verifyAuth,
        removeUser,
        isLogin
    };
})

